package com.school.revervationSys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.revervationSys.dto.IdDto;
import com.school.revervationSys.dto.LoginDto;
import com.school.revervationSys.dto.PasswordUpdateDto;
import com.school.revervationSys.dto.UserDto;
import com.school.revervationSys.entity.User;
import com.school.revervationSys.service.IUserService;
import com.school.revervationSys.utils.BaseResponse;
import com.school.revervationSys.utils.ErrorCode;
import com.school.revervationSys.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    private IUserService userService;

    @PostMapping("/addUser")
    public BaseResponse addUser(@RequestBody User user){

        boolean b = userService.addUser(user);
        if(b){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"操作失败");
    }
    @PostMapping("/updateUser")
    public BaseResponse updateUser(@RequestBody User user){

        boolean b = userService.updateUser(user);
        if(b){
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"修改失败");
    }

    @GetMapping("/delUser")
    public BaseResponse delUser(Integer userId){

        boolean b = userService.delete(userId);
        if(b){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"删除失败");
    }

    @GetMapping("/useInfo")
    public BaseResponse useInfo(Integer userId) {

        User user = userService.userInfo(userId);

        return ResultUtils.success(user);
    }
    @PostMapping("/batchDelUser")
    public BaseResponse batchDelUser(@RequestBody IdDto param){

        boolean b = userService.batchDelete(param.getIds());
        if(b){
            return ResultUtils.success("批量删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"批量删除失败");
    }

    @PostMapping("/userList")
    public BaseResponse userList(@RequestBody UserDto param){
        PageHelper.startPage(param.getPageNo(),param.getPageSize());
        List<User> userPage = userService.userList(param);
        PageInfo<User> userPageInfo = new PageInfo<>(userPage);
        return ResultUtils.success(userPageInfo);
    }

    @PostMapping("/updatePassword")
    public BaseResponse updatePassword(@RequestBody PasswordUpdateDto param){
         return   userService.updatePassword(param);
    }

    @PostMapping("/login")
    public BaseResponse login(@RequestBody LoginDto param){

        return userService.login(param);
    }



}
